package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.*;
import com.jinzhi.jzweb.pojo.ExaminationSingleScoreVo;
import com.jinzhi.jzweb.service.*;
import com.jinzhi.sys.domain.UserDO;
import com.jinzhi.sys.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 
 * <pre>
 * 技能考试单题分数详情信息表
 * </pre>
 * <small> 2020-06-05 09:41:09 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/examinationSingleScore")
public class ExaminationSingleScoreController extends BaseController {
	@Autowired
	private ExaminationSingleScoreService examinationSingleScoreService;
	@Autowired
	private StuSignUpService stuSignUpService;
	@Autowired
	private UserService userService;
	@Autowired
	private ItemBankService itemBankService;
	@Autowired
	private ExaminationGradingService examinationGradingService;
	@Autowired
	private ExaminationBatchService examinationBatchService;

	@GetMapping()
	String ExaminationSingleScore(){
		return "jzweb/examinationSingleScore/examinSumScore";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:examinationSingleScore:examinationSingleScore")
	public Result<Page<ExaminationSingleScoreVo>> list(HttpServletRequest req , int pageNumber, int pageSize, String stuSignName, String enterExpertName, String ebatchName){
		Map map = new HashMap();
		//查询用户角色
		String roleName = getRoleNames();
		if (!roleName.contains("超级用户角色")) {
			if(roleName.contains("考评站") || roleName.contains("考核站点")){
				List<Long> stuSignIds = stuSignUpService.selOrgStuAll(this.getUser().getOrgId());
				if(stuSignIds != null && stuSignIds.size() > 0){
					map.put("stuSignId", stuSignIds);
				}
			}else{
				Object orgId = req.getSession().getAttribute("orgId");
				List<Long> stuSignIds;
				if (orgId != null){
					stuSignIds = stuSignUpService.selOrgStuAll((Long) orgId);
				}else{
					stuSignIds = stuSignUpService.selOrgStuAll(this.getUser().getOrgId());
				}

				if(stuSignIds != null && stuSignIds.size() > 0){
					map.put("stuSignId", stuSignIds);
				}
				map.put("enterExpertId", this.getUserId());
			}
		}
		map.put("stuSignName", stuSignName);
		map.put("enterExpertName",enterExpertName);
		map.put("ebatchName",ebatchName);
		Page<ExaminationSingleScoreVo> page = examinationSingleScoreService.selExaminSumScore(pageNumber,pageSize,map);
		return Result.ok(page);
	}
	
	@GetMapping("/infoList")
	@RequiresPermissions("jzweb:examinationSingleScore:examinationSingleScore")
	String infoList(Model model,String stuSignId,String enterExpertId,String ebatchId){
		model.addAttribute("stuSignId",stuSignId);
		model.addAttribute("enterExpertId",enterExpertId);
		model.addAttribute("ebatchId",ebatchId);
	    return "jzweb/examinationSingleScore/examinationSingleScore";
	}
	
	@ResponseBody
	@GetMapping("/infoLists")
	@RequiresPermissions("jzweb:examinationSingleScore:examinationSingleScore")
	public Result<Page<ExaminationSingleScoreDO>> infoLists(String stuSignId,String enterExpertId,String ebatchId,String ibankName,String accPointName){
		Wrapper<ExaminationSingleScoreDO> wrapper = new EntityWrapper<ExaminationSingleScoreDO>().orderBy("id", false);
		wrapper.eq("stu_sign_id", Long.parseLong(stuSignId));
		wrapper.eq("enter_expert_id", Long.parseLong(enterExpertId));
		wrapper.eq("ebatch_id", Long.parseLong(ebatchId));
		if (StringUtils.isNotBlank(ibankName)) {
			Wrapper<ItemBankDO> itemWrapper = new EntityWrapper<ItemBankDO>().like("topic_content", ibankName);
			List<ItemBankDO> itemBankDOS = itemBankService.selectList(itemWrapper);
			String str = "";
			for (ItemBankDO itemBankDO : itemBankDOS) {
				str = str + itemBankDO.getId() + ",";
			}
			wrapper.and();
			if (StringUtils.isNotBlank(str)) {
				wrapper.in("ibank_id", str.substring(0, str.length() - 1));
			} else {
				wrapper.eq("ibank_id", null);
			}
		}
		if (StringUtils.isNotBlank(accPointName)) {
			Wrapper<ExaminationGradingDO> exaWrapper = new EntityWrapper<ExaminationGradingDO>().like("acc_point", accPointName);
			List<ExaminationGradingDO> examinationGradingDOS = examinationGradingService.selectList(exaWrapper);
			String str = "";
			for (ExaminationGradingDO examinationGradingDO : examinationGradingDOS) {
				str = str + examinationGradingDO.getId() + ",";
			}
			wrapper.and();
			if (StringUtils.isNotBlank(str)) {
				wrapper.in("egrading_id", str.substring(0, str.length() - 1));
			} else {
				wrapper.eq("egrading_id", null);
			}
		}
		Page<ExaminationSingleScoreDO> page = examinationSingleScoreService.selectPage(getPage(ExaminationSingleScoreDO.class), wrapper);
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
			ExaminationSingleScoreDO obj = (ExaminationSingleScoreDO) page.getRecords().get(i);
			if (obj.getEbatchId() != null) {
				ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(obj.getEbatchId());
				if (examinationBatchDO != null) {
					obj.setEbatchName(examinationBatchDO.getName());
				}
			}
			if (obj.getEnterExpertId() != null) {
				UserDO userDO = userService.selectById(obj.getEnterExpertId());
				if (userDO != null) {
					obj.setEnterExpertName(userDO.getName());
				}
			}
			if (obj.getStuSignId() != null) {
				StuSignUpDO stuSignUpDO = stuSignUpService.selectById(obj.getStuSignId());
				if (stuSignUpDO != null) {
					obj.setStuSignName(stuSignUpDO.getName());
				}
			}
			if (obj.getIbankId() != null) {
				ItemBankDO itemBankDO = itemBankService.selectById(obj.getIbankId());
				if (itemBankDO != null) {
					obj.setIbankName(itemBankDO.getTopicContent());//题目内容
				}
			}
			if (obj.getEgradingId() != null) {
				ExaminationGradingDO examinationGradingDO = examinationGradingService.selectById(obj.getEgradingId());
				if (examinationGradingDO != null) {
					obj.setAccPointName(examinationGradingDO.getAccPoint());//考核站点
					obj.setEgradingName(examinationGradingDO.getRatingClaim());//评分要求
				}
			}
			if (obj.getUpdaterId() != null) {
				UserDO userDO = userService.selectById(obj.getUpdaterId());
				if (userDO != null) {
					obj.setUpdaterName(userDO.getName());
				}
			}
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:examinationSingleScore:add")
	String add(){
	    return "jzweb/examinationSingleScore/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:examinationSingleScore:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ExaminationSingleScoreDO examinationSingleScore = examinationSingleScoreService.selectById(id);
		if (examinationSingleScore.getEbatchId() != null) {
			ExaminationBatchDO examinationBatchDO = examinationBatchService.selectById(examinationSingleScore.getEbatchId());
			if (examinationBatchDO != null) {
				examinationSingleScore.setEbatchName(examinationBatchDO.getName());
			}
		}
		if (examinationSingleScore.getEnterExpertId() != null) {
			UserDO userDO = userService.selectById(examinationSingleScore.getEnterExpertId());
			if (userDO != null) {
				examinationSingleScore.setEnterExpertName(userDO.getName());
			}
		}
		if (examinationSingleScore.getStuSignId() != null) {
			StuSignUpDO stuSignUpDO = stuSignUpService.selectById(examinationSingleScore.getStuSignId());
			if (stuSignUpDO != null) {
				examinationSingleScore.setStuSignName(stuSignUpDO.getName());
			}
		}
		if (examinationSingleScore.getIbankId() != null) {
			ItemBankDO itemBankDO = itemBankService.selectById(examinationSingleScore.getIbankId());
			if (itemBankDO != null) {
				examinationSingleScore.setIbankName(itemBankDO.getTopicContent());//题目内容
			}
		}
		if (examinationSingleScore.getEgradingId() != null) {
			ExaminationGradingDO examinationGradingDO = examinationGradingService.selectById(examinationSingleScore.getEgradingId());
			if (examinationGradingDO != null) {
				examinationSingleScore.setAccPointName(examinationGradingDO.getAccPoint());//考核站点
				examinationSingleScore.setEgradingName(examinationGradingDO.getRatingClaim());//评分要求
			}
		}
		model.addAttribute("examinationSingleScore", examinationSingleScore);
	    return "jzweb/examinationSingleScore/edit";
	}
	
	@Log("添加技能考试单题分数详情信息表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:examinationSingleScore:add")
	public Result<String> save( ExaminationSingleScoreDO examinationSingleScore){
		examinationSingleScoreService.insert(examinationSingleScore);
        return Result.ok();
	}
	
	@Log("修改技能考试单题分数详情信息表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:examinationSingleScore:edit")
	public Result<String>  update( ExaminationSingleScoreDO examinationSingleScore){
		examinationSingleScore.setUpdaterId(this.getUserId());
		boolean update = examinationSingleScoreService.updateById(examinationSingleScore);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除技能考试单题分数详情信息表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationSingleScore:remove")
	public Result<String>  remove( Long id){
		examinationSingleScoreService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除技能考试单题分数详情信息表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:examinationSingleScore:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		examinationSingleScoreService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
